package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tanhua.commons.Constants;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.CommentLikeApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.CommentLike;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentService {

    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private CommentLikeApi commentLikeApi;
    @Autowired
    private MqMessageService mqMessageService;

    @Autowired
    private UserFreezeService userFreezeService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public PageResult getCommentList(String movementId, Long page, Long pageSize) {
        PageResult<Comment> comments = commentApi.pageCommentList(movementId, page, pageSize);
        List<CommentVo> commentVos = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(comments.getItems())) {
            List<Long> userIds = CollUtil.getFieldValues(comments.getItems(), "userId", Long.class);
            Map<Long, UserInfo> userInfoMap = userInfoApi.getBatchIdsByCondition(userIds, null);

            comments.getItems().forEach(comment -> {
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                if(null != userInfo) {
                    commentVos.add(CommentVo.init(userInfo, comment));
                }
            });
        }

        return new PageResult(page, pageSize, comments.getCounts(), commentVos);
    }

    /**
     * 添加评论
     * @param params
     */
    public void addComment(Map<String, String> params) {
        Long userId = UserHolder.getUserId();
        userFreezeService.checkUserStates(userId, Constants.USER_FREEZE_COMMENT);

        Comment comment = new Comment();
        comment.setContent(params.get("comment"));
        comment.setPublishId(new ObjectId(params.get("movementId")));
        comment.setUserId(userId);
        comment.setCommentType(2);

        commentApi.publish(comment);
        mqMessageService.sendLogMessage(userId, "0205", "movement", comment.getPublishId().toHexString());
    }

    /**
     * 评论点赞
     * @param commentId
     * @return
     */
    public Integer like(String commentId) {
        Long userId = UserHolder.getUserId();
        CommentLike commentLike = new CommentLike();
        commentLike.setUserId(userId);
        commentLike.setCommentId(commentId);
        Integer likeCount = commentLikeApi.save(commentLike);

        String key = Constants.COMMENT_INTERACT_KEY + commentId;
        String hashKey = Constants.COMMENT_LIKE_HASHKEY + userId;
        redisTemplate.opsForHash().put(key, hashKey, "1");

        return likeCount;
    }

    /**
     * 评论取消点赞
     * @param commentId
     * @return
     */
    public Integer dislike(String commentId) {
        Long userId = UserHolder.getUserId();
        CommentLike commentLike = new CommentLike();
        commentLike.setUserId(userId);
        commentLike.setCommentId(commentId);
        Integer likeCount = commentLikeApi.remove(commentLike);

        String key = Constants.COMMENT_INTERACT_KEY + commentId;
        String hashKey = Constants.COMMENT_LIKE_HASHKEY + userId;
        redisTemplate.opsForHash().delete(key, hashKey);

        return likeCount;
    }
}
